<?php

/**
 *  @FileComment            [商品类型的公共模型]
 *  @ProjectName             项目名称：shopFrame
 *  @FileName                   文件名称：TypeModel.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-9 17:21:53
 *  *
 */
class TypeModel extends Model {

        public $table = 'type';
        public $validate = array(
            array('tname', 'nonull', '类型名称不能为空', 2, 3),
            array('tsort', 'nonull', '类型分组ID不能为空', 2, 3)
        );

        public function add_type() {
                return $this->add();
        }

        public function get_all($group = NULL) {
                return $this->order('tsort')->group($group)->all();
        }

        public function del_type($tid) {
                $data = K('TypeAttr')->where(array('type_tid' => $tid))->find();
                if ($data) {
                        $this->error = '请先删除该类型的规格属性';
                        return false;
                }
                $data2 = K('Category')->where(array('type_tid' => $tid))->find();
                if ($data2) {
                        $this->error = '请先删除该类型下的分类';
                        return false;
                }
                $this->where(array('tid' => $tid))->del();
                return true;
        }

        public function get_one_data($tid) {
                return $this->where(array('tid' => $tid))->find();
        }

        public function edit_data() {
                return $this->update();
        }

}
